package com.xxh.dasongapp.utils;

import org.springframework.util.FileSystemUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class FileUtils {

    public static String saveFile(MultipartFile uploadFile, String path) throws IOException {
        //上传原文件名
        String uploadName = uploadFile.getOriginalFilename();
        //后缀名获取
        String exts = "."+uploadName.substring(uploadName.lastIndexOf(".") + 1);
        //新文件名
        String fileName  = UUID.randomUUID().toString().trim().replaceAll("-", "").concat(exts);
        //保存文件
        File file = new File(path + fileName);

        // 如果不存在则创建目录
        if(!file.exists()){
            file.mkdirs();
        }
        uploadFile.transferTo(file);

        return fileName;
    }

    public static Boolean removeFile(String fileName, String path) {
        String filePath = path + fileName;
        FileSystemUtils.deleteRecursively(new File(filePath));
        return true;
    }


}
